package com.blog.cmrpersonalblog.service.impl;

import com.blog.cmrpersonalblog.dto.file.respnose.FileUploadResponse;
import com.blog.cmrpersonalblog.enums.FileUploadType;
import com.blog.cmrpersonalblog.factory.FileUploadStrategyFactory;
import com.blog.cmrpersonalblog.service.UnifiedFileUploadService;
import com.blog.cmrpersonalblog.strategy.upload.FileUploadStrategy;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * 统一文件上传服务实现类
 */
@Slf4j
@Service
public class UnifiedFileUploadServiceImpl implements UnifiedFileUploadService {
    
   @Resource
    private FileUploadStrategyFactory strategyFactory;
    
    @Override
    public FileUploadResponse uploadFile(MultipartFile file, FileUploadType uploadType, Long userId, Object... extraParams) {
        log.info("开始上传文件: type={}, userId={}, fileName={}, size={}", 
            uploadType.getDescription(), userId, file.getOriginalFilename(), file.getSize());
        
        try {
            // 获取对应的上传策略
            FileUploadStrategy strategy = strategyFactory.getStrategy(uploadType);
            
            // 执行上传
            FileUploadResponse response = strategy.upload(file, userId, extraParams);
            
            log.info("文件上传成功: type={}, fileName={}, fileUrl={}", 
                uploadType.getDescription(), response.getFileName(), response.getFileUrl());
            
            return response;
            
        } catch (Exception e) {
            log.error("文件上传失败: type={}, userId={}, fileName={}, error={}", 
                uploadType.getDescription(), userId, file.getOriginalFilename(), e.getMessage(), e);
            throw new RuntimeException("文件上传失败：" + e.getMessage());
        }
    }
    
    @Override
    public FileUploadResponse uploadFile(MultipartFile file, String typeCode, Long userId, Object... extraParams) {
        FileUploadType uploadType = FileUploadType.fromCode(typeCode);
        return uploadFile(file, uploadType, userId, extraParams);
    }
    
    @Override
    public boolean deleteFile(String relativePath, FileUploadType uploadType) {
        log.info("开始删除文件: type={}, path={}", uploadType.getDescription(), relativePath);
        
        try {
            // 获取对应的上传策略
            FileUploadStrategy strategy = strategyFactory.getStrategy(uploadType);
            
            // 执行删除
            boolean success = strategy.deleteFile(relativePath);
            
            if (success) {
                log.info("文件删除成功: type={}, path={}", uploadType.getDescription(), relativePath);
            } else {
                log.warn("文件删除失败: type={}, path={}", uploadType.getDescription(), relativePath);
            }
            
            return success;
            
        } catch (Exception e) {
            log.error("文件删除异常: type={}, path={}, error={}", 
                uploadType.getDescription(), relativePath, e.getMessage(), e);
            return false;
        }
    }
    
    @Override
    public boolean deleteFile(String relativePath, String typeCode) {
        FileUploadType uploadType = FileUploadType.fromCode(typeCode);
        return deleteFile(relativePath, uploadType);
    }
    
    @Override
    public boolean validateFile(MultipartFile file, FileUploadType uploadType) {
        try {
            // 获取对应的上传策略
            FileUploadStrategy strategy = strategyFactory.getStrategy(uploadType);
            
            // 执行验证
            return strategy.validateFile(file);
            
        } catch (Exception e) {
            log.error("文件验证异常: type={}, fileName={}, error={}", 
                uploadType.getDescription(), file.getOriginalFilename(), e.getMessage(), e);
            return false;
        }
    }
    
    @Override
    public boolean validateFile(MultipartFile file, String typeCode) {
        FileUploadType uploadType = FileUploadType.fromCode(typeCode);
        return validateFile(file, uploadType);
    }
}
